<?php 
require_once 'BaseController.php';
class Admin_TvonlineController extends BaseController
{
    public function init()
    {
        $this->view->module = $this->_request->getModuleName();
        $this->view->controller = $this->_request->getControllerName();
        $this->view->action = $this->_request->getActionName();
        
        $this->_arrParam = $this->_request->getParams();
		
		//Bo di cac truong ko can thiet
		unset($this->_arrParam['module']);
		unset($this->_arrParam['controller']);
		unset($this->_arrParam['action']);

        Zend_Loader::loadClass('AuthModel');
        Zend_Loader::loadClass('AdminModel');
        Zend_Loader::loadClass('TVOnlineModel');
        
    }
    public function indexAction()
    {
        if (isset($_GET['ajax'])) {
            $options = array('json' => 1);
            $model = new TVOnlineModel();
            echo $model->ListForDB('', $options);
            exit();
        }
    }
    public function addAction()
    {
        if ($this->_request->isPost()) {
            $params = $this->_arrParam;
            $this->view->arrParam = $params;
            $ModelTV = new TVOnlineModel();
            if(!$params['name'] || !$params['logo']){
                $warming = "Dữ liệu điền chưa đầy đủ";
                $this->view->warming = $warming;
            }else{
                if($params['play_default'] == '1'){
                    $default_old = $ModelTV->listChecked();
                    foreach($default_old as $item){
                        $item['play_default'] = '0';
                        $ModelTV->update($item,"id=".$item['id']);
                    }
                }
                $data = array(
                    "name" => $params['name'],
                    "logo" => stripslashes($params['logo']),
                    "code" => stripslashes($params['code']),
                    "code_mobile" => stripslashes($params['code_mobile']),
                    "type" => $params['type'],
                    "description" => $params['description'],
                    "play_default" => $params['play_default'],
                    "status" => $params['status']
                );
                
                $ModelTV->insert($data);
                $success = "Thêm thành công";
                $this->view->success = $success;
            }
        }
    }
    public function editAction()
    {
        $params = $this->_arrParam;
        $ModelTV = new TVOnlineModel();
        if ($this->_request->isPost()) {
            $this->view->arrParam = $params;
            if(!$params['name'] || !$params['logo']){
                $warming = "Dữ liệu điền chưa đầy đủ";
                $this->view->warming = $warming;
            }else{
                if($params['play_default'] == '1'){
                    $default_old = $ModelTV->listChecked();
                    foreach($default_old as $item){
                        $item['play_default'] = '0';
                        $ModelTV->update($item,"id=".$item['id']);
                    }
                }
                $data = array(
                    "name" => $params['name'],
                    "logo" => stripslashes($params['logo']),
                    "code" => stripslashes($params['code']),
                    "code_mobile" => stripslashes($params['code_mobile']),
                    "type" => $params['type'],
                    "description" => $params['description'],
                    "play_default" => $params['play_default'],
                    "status" => $params['status']
                );
                
                $ModelTV->update($data,"id=".$params['id']);
                $success = "Sửa thành công";
                $this->view->success = $success;
            }
        }
        $arrParam = $ModelTV->fetchRow("id=".$params['id']);
        $this->view->arrParam = $arrParam;
    }
    public function deleteAction()
    {
        $rows = $this->_request->getParam('checks');
        $model = new TVOnlineModel();

        $model->multiDelete($rows);
        $this->_redirect('/admin/tvonline/index');
        exit();
    }
}
?>